package com.taotao.cart.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.taotao.cart.bean.Item;
import com.taotao.cart.pojo.Cart;
import com.taotao.common.utils.CookieUtils;

/**
 * 未登录状态下购物车的Cookie实现
 * @author Henry
 *
 */
@Service
public class CartCookieService {

	@Autowired
	private ItemService itemService;
	/**
	 * 用户未登录状态下，查询购物车列表
	 * 去cookie中查询
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public List<Cart> queryCartListByUserId(HttpServletRequest request) throws JsonParseException, JsonMappingException, IOException {
		List<Cart> list = new ArrayList<Cart>();
		Map<Long, Cart> map = this.getCookieCartMap(request);
		if(map != null && !map.isEmpty()){
			for(Long key : map.keySet()){
				list.add(map.get(key));
			}
		}
		return list;
	}
	/**
	 * 用户未登录状态下，添加商品到购物车
	 * 逻辑： 判断用户的购物车中是否存在该商品，如果存在，数量相加，不存在，直接新增
	 * 在cookie中存一个json格式的Map<itemId,Cart>
	 * @param itemId
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	public void addItemToCart(HttpServletRequest request,
			HttpServletResponse response,Long itemId) throws JsonParseException, JsonMappingException, IOException {
		Map<Long, Cart> map = getCookieCartMap(request);
		Cart record = null;
		if(map != null && !map.isEmpty()){
			//判断用户的购物车中是否存在该商品
			record = map.get(itemId);
		}else{
			map = new HashMap<Long,Cart>();
		}
		
		if(record != null){
			record.setNum(record.getNum() + 1);;
		}else{
			record = new Cart();
			record.setItemId(itemId);
			//说明购物车中不存在该商品，直接新增
			record.setCreated(new Date());
			record.setUpdated(record.getCreated());
			record.setNum(1); //TODO 添加的商品数量需要前端页面传递过来
			Item item = this.itemService.queryItemById(itemId);
			record.setItemImage(item.getImages()[0]);//商品主图
			record.setItemPrice(item.getPrice());
			record.setItemTitle(item.getTitle());
		}
		map.put(itemId, record);
		CookieUtils.setCookie(request, response, PropertiesService.TT_CART_COOKIE, PropertiesService.MAPPER.writeValueAsString(map));
		
	}

	/**
	 * 获取Cookie中的 TT_CART 对应的数据(未登录的购物车数据)
	 * @param request
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public Map<Long,Cart> getCookieCartMap(HttpServletRequest request) throws JsonParseException, JsonMappingException, IOException{
		Map<Long,Cart> map = null;
		//获取Cookie中的 TT_CART 对应的数据(未登录的购物车数据)
		String jsonMap = CookieUtils.getCookieValue(request, PropertiesService.TT_CART_COOKIE);
		if(StringUtils.isNotEmpty(jsonMap)){
			TypeFactory typeFactory = PropertiesService.MAPPER.getTypeFactory();
			MapType mapType = typeFactory.constructMapType(HashMap.class, Long.class, Cart.class);
			 map = PropertiesService.MAPPER.readValue(jsonMap, mapType);
		}
		return map;
	}
	
	/**
	 * 更新购物车商品数量
	 * @param request
	 * @param itemId
	 * @param num
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public void updateCart(HttpServletRequest request,HttpServletResponse response,
			Long itemId, Integer num) throws JsonParseException, JsonMappingException, IOException {
		Map<Long, Cart> map = this.getCookieCartMap(request);
		Cart cart = map.get(itemId);
		cart.setNum(num);
		map.put(itemId, cart);
		String jsonMap = PropertiesService.MAPPER.writeValueAsString(map);
		CookieUtils.setCookie(request, response, PropertiesService.TT_CART_COOKIE, jsonMap);
	}
	
	public void deleteItemToCart(HttpServletRequest request,
			HttpServletResponse response, Long itemId) throws JsonParseException, JsonMappingException, IOException {
		Map<Long, Cart> map = this.getCookieCartMap(request);
		map.remove(itemId);
		String jsonMap = PropertiesService.MAPPER.writeValueAsString(map);
		CookieUtils.setCookie(request, response, PropertiesService.TT_CART_COOKIE, jsonMap);
	}
	
	
	
	
	
	
	
	
	
	
}
